﻿Imports System.Collections.Specialized

Public Class frmTabAttributeAccuracy

    Dim EditNode As EditXML = New EditXML()
    Public Shared sAttribAcc As StringCollection = New StringCollection()
    Public Shared sPdelim As String = "£"

    Private Sub txtAttribAcc_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAttribAcc.Leave
        editXMLFile()
    End Sub
    Public Sub editXMLFile()
        EditNode.Edit("/metadata/dataqual/attracc/attraccr", txtAttribAcc.Text.Trim())

        EditNode.Remove("/metadata/dataqual/attracc", "qattracc")
        For i As Integer = sAttribAcc.Count To 1 Step -1
            Dim values() As String = sAttribAcc(i - 1).Split(sPdelim)
            EditNode.Add("/metadata/dataqual/attracc", "/attraccr", "qattracc", "<attraccv>" & values(0) & "</attraccv><attracce>" & values(1) & "</attracce>")
        Next
    End Sub
    Private Sub btnAddAttribAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAttribAcc.Click
        Dim sNewAttrib As String = sPdelim 'create blank attribute accuracy assessment pair
        Dim frmNewAttrib As New frmAttribAcc(sNewAttrib)
        frmNewAttrib.ShowDialog()
        If frmNewAttrib.getUpdated() Then 'add the new assessment
            sNewAttrib = frmNewAttrib.getAttrib()
            sAttribAcc.Add(sNewAttrib)
            sNewAttrib = Replace(sNewAttrib, sPdelim, ":  ")
            lboxAttribAcc.Items.Add(sNewAttrib)
            editXMLFile()
        End If
    End Sub

    Private Sub btnEditAttribAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditAttribAcc.Click
        Dim iCount As Integer = lboxAttribAcc.Items.Count
        Dim iSelInd As Integer = lboxAttribAcc.SelectedIndex 'get selected assessment to delete
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No Attribute Assessment selected for editing!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim sNewAttrib As String = sAttribAcc(iSelInd) 'retrieve selected attribute accuracy assessment pair
        Dim frmEditAttrib As New frmAttribAcc(sNewAttrib)
        frmEditAttrib.ShowDialog()
        If frmEditAttrib.getUpdated() Then 'update the assessment
            sNewAttrib = frmEditAttrib.getAttrib()
            sAttribAcc.RemoveAt(iSelInd) 'remove old version
            sAttribAcc.Insert(iSelInd, sNewAttrib) 'put new version into collection at same point
            sNewAttrib = Replace(sNewAttrib, sPdelim, ":  ")
            lboxAttribAcc.Items.RemoveAt(iSelInd) 'update list box -- remove old version
            lboxAttribAcc.Items.Insert(iSelInd, sNewAttrib) 'update list box -- insert new version
            editXMLFile()
        End If
        btnEditAttribAcc.Enabled = False
        btnDelAttribAcc.Enabled = False
    End Sub

    Private Sub btnDelAttribAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelAttribAcc.Click
        Dim iSelInd As Integer = lboxAttribAcc.SelectedIndex 'get selected assessment to delete
        If (iSelInd = -1) Then
            MessageBox.Show("No Attribute Assessment selected for deletion!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        sAttribAcc.RemoveAt(iSelInd) 'update string collection
        lboxAttribAcc.Items.RemoveAt(iSelInd) 'update list box
        editXMLFile()
        btnEditAttribAcc.Enabled = False
        btnDelAttribAcc.Enabled = False
    End Sub

    Private Sub lboxAttribAcc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxAttribAcc.SelectedIndexChanged
        If lboxAttribAcc.SelectedItems.Count > 0 Then btnEditAttribAcc.Enabled = True : btnDelAttribAcc.Enabled = True Else btnEditAttribAcc.Enabled = False : btnDelAttribAcc.Enabled = False
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("AttributeAccuracy")
        helpForm.ShowDialog()
    End Sub
End Class